import config from 'config'
import httpFetch from 'share/httpFetch'
import errorMessage from 'share/errorMessage';
import axios from "axios";
import LoginService from 'containers/login/login.service';

export default {

  //获取验证码
  getConfirmCode(login, attachmentOID, verifyCode) {
    /*
   * @apiParam {String} login 账号
     * @apiParam {String} attachmentOID 附件oid
     * @apiParam {String} verifyCode 验证码
     * @apiParam {String} client 客户端
     */
    let params = {
      client: "web",
      login: LoginService.enCodePassword(login),
      verifyCode,
      attachmentOID
    };
    let time = (new Date()).getTime();
    return httpFetch.get(`${config.baseUrl}/api/refactor/activation/email/or/mobile/token?time=${time}`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  // 获取密码设置规则
  getPasswordRule: function (mobile_or_email) {
    //建议能用邮箱获取,就邮箱,因为手机可能被修改
    return LoginService.getPasswordRuleV2(mobile_or_email);
  },

  //判断验证码是否正确
  judgeConfirmCode(login, token) {
    /*
* @apiParam
    * @apiParam {String} login 手机号码或邮箱
    * @apiParam {String} token 验证码
    * @apiParam {String} newPassword 密码
    * @apiParam {String} client 客户端
    */
    let params = {
      login: LoginService.enCodePassword(login),
      token,
      client: "web",
    };
    return new Promise((resolve, reject) => {
      return axios({
        url: config.baseUrl + `/api/refactor/activation/email/or/mobile/check`,
        method: 'POST',
        data: params,
      }).then(res => {
        resolve(res);
      }).catch(err => {
        errorMessage(err.response);
        reject(err.response);
      });
    })
  },

  //确认新密码
  confirmNewPassword(login, token, newPassword) {
    /*
 * @apiParam
     * @apiParam {String} login 手机号码或邮箱
     * @apiParam {String} token 验证码
     * @apiParam {String} newPassword 密码
     * @apiParam {String} client 客户端
     */
    let params = {
      login: LoginService.enCodePassword(login),
      token,
      client: "web",
      newPassword: LoginService.enCodePassword((newPassword))
    };
    return new Promise((resolve, reject) => {
      return axios({
        url: config.baseUrl + `/api/refactor/activate/email/or/mobile`,
        method: 'POST',
        data: params,
      }).then(res => {
        resolve(res);
      }).catch(err => {
        errorMessage(err.response);
        reject(err.response);
      });
    })
  },

  //获取汇联易协议
  getHuilianyiContact(phoneNum) {
    return LoginService.getPasswordRuleV2(phoneNum)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //获取自定义协议
  getCustomContact(id) {
    return httpFetch.unsafe.get(`${config.baseUrl}/api/tenant/protocol/id?id=${id}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //获取图片验证码
  getVerifyCodeImage() {
    let time = (new Date()).getTime();
    return httpFetch.get(`${config.baseUrl}/api/refactor/user/verify/code?time=${time}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
}
